package  com.lepu.app.fun.tool.ymsjb.remind;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

public class AlarmTipsManager {
	 private static final String TABLE_NAME = "alarm";
    /**
     * 插入定时器 列表
     *
     * @param
     */
	 
	 
    public static boolean insertAlarmList(List<Alarm> alarmList) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;
        if(null != alarmList && 0 < alarmList.size()){
        	for(int i = 0; i < alarmList.size(); i++){
        		Alarm item = alarmList.get(i);
        		ContentValues values = new ContentValues();
        		values.put("uid", item.uid);
                values.put("alarmId", item.alarmId);
                values.put("alarmTime", item.alarmTime);
                values.put("setTime", item.setTime);
                values.put("showTime", item.showTime);
                values.put("note", item.note);
                
                flag = dbHelp.insert(TABLE_NAME, null, values);
        	}
        }

        return flag;
    }

    /**
     * 插入定时器
     *
     * @param
     */
    public static boolean insertAlarm(Alarm item) {
    	 boolean flag = false;

         DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
         if (dbHelp == null)
             return flag;
         
		ContentValues values = new ContentValues();
		values.put("uid", item.uid);
        values.put("alarmId", item.alarmId);
        values.put("alarmTime", item.alarmTime);
        values.put("setTime", item.setTime);
        values.put("showTime", item.showTime);
        values.put("note", item.note);
        flag = dbHelp.insert(TABLE_NAME, null, values);
    	
        return flag;
        
    }


    //获取所有数据
    public static ArrayList<Alarm> getAlarmByUid(String uid) {
    	ArrayList<Alarm> provinces = new ArrayList<Alarm>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
					"id","uid", "alarmId", "alarmTime", "setTime", "showTime", "note"}, 
					"uid=?",new String[] {uid}, null, null,null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //检查是否存在
    public static boolean checkExist(String uid, int alarmId) {
    	boolean isExist = false;
    	ArrayList<Alarm> provinces = new ArrayList<Alarm>();
    	DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
    	if (dbHelp == null){
    		isExist = true;
    		return isExist;
    	}
    	
    	SQLiteDatabase dbSqlite = null;
    	try {
    		dbSqlite = dbHelp.getWritableDatabase();
    		
    		Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
    				"id","uid", "alarmId", "alarmTime", "setTime", "showTime", "note"}, 
    				"uid=? and alarmId=?",new String[] {uid, String.valueOf(alarmId)}, null, null,null);
    		
    		cursorParse(provinces, queryResult);
    		dbSqlite.close();
    	} catch (Exception e) {
    	}
    	
    	if(provinces.size() > 0){
    		isExist = true;
    	}
    	return isExist;
    }
    
    //获取闹钟药品分类
//    public static ArrayList<Alarm> getAlarmByGroupName() {
//		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//		if (dbHelp == null)
//			return null;
//
//		String sql = "SELECT * from alarm  GROUP BY yaoName order by id desc";
//		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,null);
//
//		if (queryResult == null || queryResult.size() == 0) {
//			return null;
//		}
//
//		ArrayList<Alarm> dataList = new ArrayList<Alarm>();
//		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
//			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
//			Alarm item = parseFromDatabase(dictionary);
//			dataList.add(item);
//		}
//		return dataList;
//	}
    
    //删除某一条记录
    public static boolean deleteItem(int alarmId) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "alarmId=?",new String[]{String.valueOf(alarmId)});

        return flag;
    }
    
    //删除某一用户
    public static boolean deleteByUid(String uid) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "uid=?",new String[]{String.valueOf(uid)});

        return flag;
    }
    
    /**
     * 更新定时器
     *
     * @param
     */
//    public void updateAlarm(Alarm alarm) {
//        DbQueryRunner queryRunner = DbQueryRunner.getInstance();
//
//        try {
//            String sql = "update alarm set alarmTime=? where id=?";
//            Object params[] = {alarm.alarmTime, alarm.id};
//            queryRunner.update(sql, params);
//        } catch (Exception e) {
//            android.util.Log.d(e.toString(), e.getMessage() == null ? "" : e.getMessage());
//        }
//    }

    /**
     * 获取最近的闹钟
     * @return
     */
	public List<Alarm> getAlarmCurrent() {

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return null;

		String sql = "SELECT * from alarm  ORDER BY alarmTime desc";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,null);

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

        List<Alarm> dataList = new ArrayList<Alarm>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			Alarm item = parseFromDatabase(dictionary);
			dataList.add(item);
		}
		return dataList;
    }

    static class AlarmManagerHolder {
        static AlarmTipsManager alarmManager = new AlarmTipsManager();
    }

    public static AlarmTipsManager getInstance() {
        return AlarmManagerHolder.alarmManager;
    }
    
	/**
	 * 解析cursor转换为对象
	 */
	private static void cursorParse(ArrayList<Alarm> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			Alarm beanAlarm = new Alarm();
			beanAlarm.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanAlarm.uid = (cursor.getString(cursor.getColumnIndex("uid")));
			beanAlarm.alarmId = (cursor.getInt(cursor.getColumnIndex("alarmId")));
			beanAlarm.alarmTime = (cursor.getLong(cursor.getColumnIndex("alarmTime")));
			beanAlarm.setTime = (cursor.getLong(cursor.getColumnIndex("setTime")));
			beanAlarm.showTime = (cursor.getString(cursor.getColumnIndex("showTime")));
			beanAlarm.note = (cursor.getString(cursor.getColumnIndex("note")));
			msgArray.add(beanAlarm);
		}
	}
	
	public static Alarm parseFromDatabase(HashMap<String, Object> dictionary) {

		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		Alarm item = new Alarm();

		item.id = UtilityBase.parseInt((String) dictionary.get("id"));
		item.uid = UtilityBase.parseString((String) dictionary.get("uid"));
		item.alarmId = UtilityBase.parseInt((String) dictionary.get("alarmId"));
		item.alarmTime = UtilityBase.parseLong((String) dictionary.get("alarmTime"));
		item.setTime = UtilityBase.parseLong((String) dictionary.get("setTime"));
		item.showTime = UtilityBase.parseString((String) dictionary.get("showTime"));
		item.note = UtilityBase.parseString((String) dictionary.get("note"));

		return item;
	}
}
